package kava.framework.demo;

import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import kava.framework.lcdui.Form;
import kava.framework.lcdui.KavaFrame;
import kava.framework.lcdui.KavaFrameUser;

public class DemoMIDlet extends MIDlet implements KavaFrameUser  {
	public static String UA;
	public static DemoMIDlet me;
	public static final int APP_MSG_CHECK_MSGBOX_LIVE = 0;
	public DemoMIDlet() {
		KavaFrame.setKavaUser(this);
		UA = (String) KavaFrame.configRes().get("userAgent");
		me = this;
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO Auto-generated method stub

	}

	protected void pauseApp() {
		// TODO Auto-generated method stub

	}

	protected void startApp() throws MIDletStateChangeException {
		Display.getDisplay(this).setCurrent(KavaFrame.getCanvas());

	}

	public void handleAppMessage(int msgCode, String msg) {
		// TODO Auto-generated method stub
		switch (msgCode) {
		case APP_MSG_CHECK_MSGBOX_LIVE:
			Form topForm = KavaFrame.peek();
			if(topForm != null && topForm.getTitle().equals("msgbox")){
				if(System.currentTimeMillis() - topForm.getTick() > 1000){
					KavaFrame.pop();
				}else{
					KavaFrame.addAppMessage(DemoMIDlet.APP_MSG_CHECK_MSGBOX_LIVE, null);
				}
			}
			break;

		default:
			break;
		}
	}

	public void launch() {
		KavaFrame.push(new MainForm());
		
	}

	public int numAlphaLevels() {
		// TODO Auto-generated method stub
		return Display.getDisplay(this).numAlphaLevels();
	}
	
	public static void exit(){
		try {
			KavaFrame.destroy();
			me.destroyApp(false);
			me.notifyDestroyed();
		} catch (MIDletStateChangeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
